package cn.gson.fengboot.model.service;

import cn.gson.fengboot.model.dao.*;
import cn.gson.fengboot.model.mapper.ZchukuMapper;
import cn.gson.fengboot.model.mapper.ZinvoiceMapper;
import cn.gson.fengboot.model.pojos.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class ZchukuService {
    @Autowired
    ZchukuMapper mapper;
    @Autowired
    ZchukuDao dao;
    @Autowired
    ZchukuxqDao xqdao;
    @Autowired
    ZdeliveryDao jhdao;
    @Autowired
    QproductdetailDao cpdao;

    public List<Zchuku> chukuliebiao(String search,String search2){
        return mapper.chukuliebiao(search,search2);
    };

    public List<Zchuku> chukutk(String search,String search2){
        return mapper.chukutk(search,search2);
    };

    public List<Zchuku> chukutk2(String search,String search2){
        return mapper.chukutk2(search,search2);
    };


    public List<Zchukuxq> chukuxq(Integer search){
        return mapper.chukuxq(search,null);
    };

    public List<Zchukuxq> chukuxq2(String search,String search2){
        return mapper.chukuxq2(search,search2);
    };


    public void add(Zchuku rk){
        Zchuku save = dao.save(rk);
        Zdelivery jf = jhdao.findById(save.getDeliveryId().getDeliveryId()).get();
        jf.setDeliveryStatic("已出库");
        jhdao.save(jf);
        save.getXq().forEach(a->{
            a.setChukuId(save);
            if(a.getChukuxqId()==0){
                Qproductdetail cp = cpdao.findById(a.getProdetailId().getProdetailId()).get();
                System.out.println(cp);
                cp.setProdetailKcnum(cp.getProdetailKcnum()-a.getChukuNumber());
                cpdao.save(cp);
            }
        });
        xqdao.saveAll(save.getXq());
    }

    //<!--谭方圆=>客户合并的出库查询-->
    public List<Zchuku> tfychukuliebiao(Integer id1, Integer id2){
        return mapper.tfychukuliebiao(id1,id2);
    }

}
